package com.aidex.biz.service.impl;

import com.aidex.biz.domain.PersonInfo;
import com.aidex.biz.mapper.PersonInfoMapper;
import com.aidex.biz.service.PersonInfoService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.sign.Md5Utils;
import com.aidex.system.service.SysDeptService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 人员信息基础Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-02-10
 */
@Service
@Transactional(readOnly = true)
public class PersonInfoServiceImpl extends BaseServiceImpl<PersonInfoMapper, PersonInfo> implements PersonInfoService {
    private static final Logger log = LoggerFactory.getLogger(PersonInfoServiceImpl.class);

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 获取单条数据
     *
     * @param personInfo 人员信息基础
     * @return 人员信息基础
     */
    @Override
    public PersonInfo get(PersonInfo personInfo) {
        PersonInfo dto = super.get(personInfo);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 人员信息基础id
     * @return 人员信息基础
     */
    @Override
    public PersonInfo get(String id) {
        PersonInfo dto = super.get(id);
        return dto;
    }


    /**
     * 查询人员信息基础列表
     *
     * @param personInfo 人员信息基础
     * @return 人员信息基础
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<PersonInfo> findList(PersonInfo personInfo) {
        return super.findList(personInfo);
    }

    /**
     * 分页查询人员信息基础列表
     *
     * @param personInfo 人员信息基础
     * @return 人员信息基础
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<PersonInfo> findPage(PersonInfo personInfo) {
        return super.findPage(personInfo);
    }

    /**
     * 保存人员信息基础
     *
     * @param personInfo
     * @return 结果
     */
    @Override
    public boolean save(PersonInfo personInfo) {
        return super.save(personInfo);
    }


    /**
     * 删除人员信息基础信息
     *
     * @param personInfo
     * @return 结果
     */
    @Override
    public boolean remove(PersonInfo personInfo) {
        return super.remove(personInfo);
    }

    /**
     * 批量删除人员信息基础
     *
     * @param ids 需要删除的人员信息基础ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deletePersonInfoByIds(String[] ids) {
        return mapper.deletePersonInfoByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param personInfo 人员信息基础
     * @return 结果
     */
    @Override
    public int findMaxSort(PersonInfo personInfo) {
        return NumberUtils.nextOrder(mapper.findMaxSort(personInfo));
    }

    @Override
    @Transactional(readOnly = false)
    public String importUser(List<PersonInfo> personInfoList) {
        if (StringUtils.isNull(personInfoList) || personInfoList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PersonInfo personInfo : personInfoList) {
            try {
                String idMd5 = Md5Utils.hash(personInfo.getPhone());
                // 验证是否存在这个用户
                PersonInfo u = mapper.get(idMd5);
                personInfo.setId(idMd5);
                SysDept sysDept = sysDeptService.getByName(personInfo.getCompanyName());
                if (StringUtils.isNotNull(sysDept)) {
                    personInfo.setDeptId(sysDept.getId());
                    personInfo.setDeptName(sysDept.getDeptName());
                }
                if (StringUtils.isNull(u)) {
                    personInfo.setIsNewRecord();
                    this.save(personInfo);
                    successNum++;

                } else {
                    this.save(personInfo);
                    successNum++;

                }

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "";
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确。");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条。：");
        }
        return successMsg.toString();
    }
}
